package com.aipp.mall.h5.service;

import com.aipp.mall.common.redis.RedisService;
import com.aipp.mall.common.utils.Json;
import com.aipp.mall.h5.domain.entity.Area;
import com.aipp.mall.h5.domain.vo.AreaVO;
import com.aipp.mall.h5.mapper.AreaMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.core.text.StrUtil;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author cc
 * @version 1.0
 * @since 2024/9/8
 */
@Service
@RequiredArgsConstructor
public class H5CommonService {

    private final RedisService redisService;

    private final AreaMapper areaMapper;

    public List<AreaVO> getAreaList() {
        String addresses = redisService.getAddressList();
        if (StrUtil.isNotEmpty(addresses)) {
            List<AreaVO> areaVOList = Json.toList(addresses, AreaVO.class);
            if (!areaVOList.isEmpty()) {
                return areaVOList;
            }
        }
        QueryWrapper<Area> addressQueryWrapper = new QueryWrapper<>();
        addressQueryWrapper.in("level", Arrays.asList(0, 1, 2));
        List<Area> areaList = areaMapper.selectList(addressQueryWrapper);
        Map<Long, List<Area>> cityMap = areaList.stream().filter(it -> it.getLevel() == 1)
                .collect(Collectors.groupingBy(Area::getParentCode));
        Map<Long, List<Area>> districtMap = areaList.stream().filter(it -> it.getLevel() == 2)
                .collect(Collectors.groupingBy(Area::getParentCode));
        List<AreaVO> result = new ArrayList<>();
        areaList.stream().filter(it -> it.getLevel() == 0).forEach(it -> {
            AreaVO dto = new AreaVO();
            dto.setId(it.getCode());
            dto.setLevel("province");
            dto.setName(it.getName());
            dto.setPid(0L);
            //获取城市列表
            List<AreaVO> child = new ArrayList<>();
            if (cityMap.containsKey(it.getCode())) {
                cityMap.get(it.getCode()).forEach(city -> {
                    AreaVO cityDto = new AreaVO();
                    cityDto.setId(city.getCode());
                    cityDto.setLevel("city");
                    cityDto.setName(city.getName());
                    cityDto.setPid(city.getParentCode());
                    cityDto.setChildren(districtMap.containsKey(city.getCode()) ?
                            districtMap.get(city.getCode()).stream().map(district -> {
                                AreaVO districtDto = new AreaVO();
                                districtDto.setId(district.getCode());
                                districtDto.setLevel("district");
                                districtDto.setName(district.getName());
                                districtDto.setPid(district.getParentCode());
                                return districtDto;
                            }).collect(Collectors.toList()) : Collections.emptyList());
                    child.add(cityDto);
                });
            }
            dto.setChildren(child);
            result.add(dto);
        });
        redisService.setAddressList(Json.toStr(result));
        return result;
    }
}
